package com.instagram.instadownloader.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.vine.vinedownloader.R;

// TODO: Auto-generated Javadoc
/**
 * The Class DialogUtil.
 */
public class DialogUtil {

	/**
	 * Show confirm dialog.
	 *
	 * @param context the context
	 * @param title the title
	 * @param message the message
	 */
	public static void showConfirmDialog(final Context context, String title,String message) {
		showConfirmDialog(context, title,message,null);
	}
	
	/**
	 * Show confirm dialog.
	 *
	 * @param context the context
	 * @param title the title
	 * @param message the message
	 * @param listener the listener
	 */
	public static void showConfirmDialog(final Context context, String title,String message,final OnDialogListener listener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		TextView mtitle = new TextView(context);
		mtitle.setText(title);
		mtitle.setGravity(Gravity.CENTER);
		mtitle.setTextSize(22);
		builder.setCustomTitle(mtitle);
		builder.setMessage(message);
		builder.setPositiveButton("OK", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(listener!=null){
					listener.onPositive(null);
				}
				dialog.dismiss();
			}
		});
		builder.setNegativeButton("Cancel", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(listener!=null){
					listener.onCancel();
				}
				dialog.dismiss();
			}
		});
		AlertDialog dialog = builder.show();
		TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
		messageText.setGravity(Gravity.CENTER);
		dialog.show();
	}
	
	/**
	 * Show confirm dialog.
	 *
	 * @param context the context
	 * @param title the title
	 * @param message the message
	 * @param listener the listener
	 */
	public static void showOpenFileDialog(final Context context, String title,String message,final OnDialogListener listener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		TextView mtitle = new TextView(context);
		mtitle.setText(title);
		mtitle.setGravity(Gravity.CENTER);
		mtitle.setTextSize(22);
		builder.setCustomTitle(mtitle);
		builder.setMessage(message);
		builder.setPositiveButton("Open File", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(listener!=null){
					listener.onPositive(null);
				}
				dialog.dismiss();
			}
		});
		builder.setNegativeButton("Later", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(listener!=null){
					listener.onCancel();
				}
				dialog.dismiss();
			}
		});
		AlertDialog dialog = builder.show();
		TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
		messageText.setGravity(Gravity.CENTER);
		dialog.show();
	}
	
	
	/**
	 * Show input dialog.
	 *
	 * @param context the context
	 * @param title the title
	 * @param path the path
	 * @param listener the listener
	 */
	public static void showInputDialog(Context context,String title,String path,final OnDialogListener listener, String fileName){
		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		View vContent = LayoutInflater.from(context).inflate(R.layout.filename_dialog, null);
		TextView tvTitle = (TextView) vContent.findViewById(R.id.tvDialogTitle);
		TextView tvPath = (TextView) vContent.findViewById(R.id.tvPath);
		final EditText edtInput = (EditText)vContent.findViewById(R.id.editText1);
		Button btnCancel = (Button)vContent.findViewById(R.id.btnCancel);
		Button btnOk = (Button)vContent.findViewById(R.id.btnOK);
		if(fileName!=null && !fileName.isEmpty()){
//			edtInput.setText(fileName);
			edtInput.setText("insta_");
		}
		tvTitle.setText(title);
		tvPath.setText(path);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(listener!=null){
					listener.onCancel();
				}
				dialog.dismiss();
			}
		});
		btnOk.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(listener!=null){
					listener.onPositive(edtInput.getText().toString());
				}
				dialog.dismiss();
			}
		});
		dialog.setContentView(vContent);
		dialog.show();
	}
	
	/**
	 * The listener interface for receiving onDialog events.
	 * The class that is interested in processing a onDialog
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addOnDialogListener<code> method. When
	 * the onDialog event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see OnDialogEvent
	 */
	public interface OnDialogListener{
		
		/**
		 * On positive.
		 *
		 * @param result the result
		 */
		public void onPositive(String result);
		
		/**
		 * On cancel.
		 */
		public void onCancel();
	}
}
